#!/usr/bin/env bash

export MILE1_HOME="$HOME/.mile1"
export MILE1_JAR="$MILE1_HOME/mile1.jar"
export MILE1_JAR_URL="http://git.oschina.net/43284683/Mile1/raw/master/downloads/mile1.jar"

# check $MILE1_JAR , if not exists , download from $MILE1_JAR_URL
# all other features delegated to clojure code
# sbt jar and sbt launch script will be installed at the same directory of this script

export JAVA_CMD="${JAVA_CMD:="java"}"

if type -p curl > /dev/null 2>&1; then
#    if ["$http_proxy" != ""]; then
#        CURL_PROXY="-x $http_proxy"
#    fi
#    HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
    HTTP_CLIENT="curl -f -L -o"
else
    HTTP_CLIENT="wget -O"
fi

if [[ ! -r $MILE1_JAR ]]; then
    echo "Installing Mile1 ..."
    mkdir -p "$(dirname $MILE1_JAR)"
    $HTTP_CLIENT "$MILE1_JAR.pending" "$MILE1_JAR_URL"
    if [ $? == 0 ]; then
        mv -f "$MILE1_JAR.pending" "$MILE1_JAR"
        echo "Mile1 installed."
    else
        rm "$MILE1_JAR.pending" 2>/dev/null
        echo "Failed to download $MILE1_JAR_URL"
        exit 1
    fi
fi

$JAVA_CMD -Dmile1.script.path="$(dirname $0)" -jar $MILE1_JAR "$@"





